#### Replication Code:
#### Goldman, Seth and Daniel J. Hopkins
#### "Past Place, Present Prejudice"
#### 
#### R code enabling analyses of
#### Youth-Parent Socialization Panel Survey 
#### Note: all Youth-Parent Socialization Panel Survey data
#### is available here (after application): https://www.icpsr.umich.edu/icpsrweb/ICPSR/studies/4037

library(Hmisc)
library(memisc)
library(doBy)
library(foreign)
library(xtable)


#### must set working directory appropriately
#setwd()

### load restricted data 
dta <- as.data.set(spss.portable.file("C:/users/danhop/Desktop/04037-0001-Data-REST.por"))
dta$SCHID <- as.numeric(as.character(dta$v7))
dta$STUID <- as.numeric(as.character(dta$v5))
length(unique(dta$STUID))

### LOAD HS COHORT DATA
### rep(1,11) goes to leading crowd
wds <- c(4,1,5,4,1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,1,1,1,rep(1,11),
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,rep(1,3),2,1,2,1,2,1,1,1,1,1,4,1,4,3,rep(1,6),1,4)
cumsum(wds)

### high school cohort data
dta.hs <- read.fwf("/YPSPS/ICPSR_07575/DS0001/07575-0001-Data.txt",widths=wds)
dta.hs$LEASTPROUD <- 1*(dta.hs$V17==1)
dta.hs$SCHID <- dta.hs$V4

dta.hs.sub <- summaryBy(data=dta.hs,LEASTPROUD ~ SCHID)

### LOAD PARENT DATA
#### note must change path below for live code on restricted machine
dta.par <- read.dta("/ICPSR_07286/DS0001/07286-0001-Data.dta")
dta.par$STUID <- dta.par$V3
sum(dta$STUID %in% dta.par$STUID)

dta.par$MOTHER <- 1*(dta.par$V318 %in% c(2))
dta.par$FATHER <- 1*(dta.par$V318 %in% c(3))

### 7 = strong gop
dta.par$PARPID <- NA
dta.par$PARPID[dta.par$V382 %in% c(1)] <- 1
dta.par$PARPID[dta.par$V382 %in% c(2)] <- 2
dta.par$PARPID[dta.par$V382 %in% c(3)] <- 3
dta.par$PARPID[dta.par$V382 %in% c(4)] <- 4
dta.par$PARPID[dta.par$V382 %in% c(5)] <- 5
dta.par$PARPID[dta.par$V382 %in% c(6)] <- 6
dta.par$PARPID[dta.par$V382 %in% c(7)] <- 7

dta.par$FTBLKSPAR <- dta.par$V443
dta.par$FTBLKSSPO <- dta.par$V451

dta.par$FTWHTSPAR <- dta.par$V441
dta.par$FTWHTSSPO <- dta.par$V449

dta.par$FTWHTSSTU <- dta.par$V197
dta.par$FTBLKSSTU <- dta.par$V199

cor(dta.par$FTBLKSSTU,dta.par$FTBLKSPAR)
# 0.1858265
cor(dta.par$FTWHTSSTU,dta.par$FTWHTSPAR)
#[1] 0.1233631

### 7 = strong gop
dta.par$PARPIDSPO <- NA
dta.par$PARPIDSPO[dta.par$V431 %in% c(1)] <- 1
dta.par$PARPIDSPO[dta.par$V431 %in% c(2)] <- 2
dta.par$PARPIDSPO[dta.par$V431 %in% c(3)] <- 3
dta.par$PARPIDSPO[dta.par$V431 %in% c(4)] <- 4
dta.par$PARPIDSPO[dta.par$V431 %in% c(5)] <- 5
dta.par$PARPIDSPO[dta.par$V431 %in% c(6)] <- 6
dta.par$PARPIDSPO[dta.par$V431 %in% c(7)] <- 7

#####################
### LOAD SCHOOL DATA

### note: must change path for live code on restricted machine
dta.sch1 <- read.dta("/users/danhop/Dropbox/national/YPSPS/ICPSR_07286/DS0002/07286-0002-Data.dta")
dta.sch1$SCHID <- dta.sch1$V2

### check with restricted data
sum(dta.sch1$SCHID %in% dta$SCHID)
sum(dta$SCHID %in% dta.sch1$SCHID)

dta.sch <- merge(dta.sch1,dta.hs.sub,by="SCHID",all.x=T)

dta.sch$PUBLICSCH <- 1*(dta.sch$V4==1)
dta.sch$PRIVATESCH <- 1*(dta.sch$V4==5)
dta.sch$CATHOLICSCH <- 1*(dta.sch$V4==8)

dta.sch$PLACESIZESCH <- dta.sch$V23

dta.sch$PCTSPANISHSCH <- NA
dta.sch$PCTSPANISHSCH[dta.sch$V24==1] <- 0
dta.sch$PCTSPANISHSCH[dta.sch$V24==2] <- 5
dta.sch$PCTSPANISHSCH[dta.sch$V24==3] <- 18
dta.sch$PCTSPANISHSCH[dta.sch$V24==4] <- 38

dta.sch$PCTBLACKSCH <- NA
dta.sch$PCTBLACKSCH[dta.sch$V27==1] <- 0
dta.sch$PCTBLACKSCH[dta.sch$V27==2] <- 5
dta.sch$PCTBLACKSCH[dta.sch$V27==3] <- 18
dta.sch$PCTBLACKSCH[dta.sch$V27==4] <- 37.5
dta.sch$PCTBLACKSCH[dta.sch$V27==5] <- 62.5
dta.sch$PCTBLACKSCH[dta.sch$V27==6] <- 82.5
dta.sch$PCTBLACKSCH[dta.sch$V27==7] <- 95
dta.sch$PCTBLACKSCH[dta.sch$V27==8] <- 100

### subset geo variables
sub <- subset(dta,select=c("v5010","v5011","v5013"))
rs <- sample(1:dim(dta)[1],replace=F)
length(unique(rs))

sub2 <- sub[order(rs),]
v10 <- unique(sub$v5010)
v11 <- unique(sub$v5011)
v13 <- unique(sub$v5013)

dta.full <- as.data.set(spss.portable.file("/YPSPS/ICPSR_04037/DS0001/04037-0001-Data.por"))

### run below with restricted data
dta.full$v5010 <- dta.full$v5011 <- dta.full$v5013 <- NULL
dta.geo.sub <- subset(dta,select=c("v5","v5010","v5011","v5013"))

#dta.geo.sub <- subset(dta.full,select=c("v5","v5010","v5011","v5013"))

dta.full$v259r <- as.character(dta.full$v259)
dta.full$v261r <- as.character(dta.full$v261)
dta.full$v344r <- as.character(dta.full$v344)

dta.full$v437r <- as.character(dta.full$v437)
dta.full$v439r <- as.character(dta.full$v439)

dta.full$v5252r <- as.character(dta.full$v5252)
dta.full$v5651r <- as.character(dta.full$v5651)
dta.full$v5611r <- as.character(dta.full$v5611)
dta.full$v5314r <- as.character(dta.full$v5314)

dta.full$v1524r <- as.character(dta.full$v1524)
dta.full$v1527r <- as.character(dta.full$v1527)

dta.full$v1233r <- as.character(dta.full$v1233)

dta.full$v5 <- as.numeric(as.character(dta.full$v5))
dta.geo.sub$v5 <- as.numeric(as.character(dta.geo.sub$v5))

vars.select <- rev(c("v6001","v5009","v5007","v5754","v1233r","v1527r","v1524r","v5314r","v5611r","v5651r",
"v5252r","v1608",
"v2023","v2025","v651","v648",
"v439r","v437r","v344r","v261r","v259r","v232","v224","v223","v222","v207","v200",
"v84",
"v7","v5"))

which(vars.select %in% colnames(dta.full))
which(! vars.select %in% colnames(dta.full))

dta.full.subset <- subset(dta.full,select=vars.select)

dta.full.A <- merge(dta.full.subset,dta.geo.sub,by=c("v5"))
dta.full.A$SCHID <- as.numeric(as.character(dta.full.A$v7))

### merge school subset
dta.sch.sub <- subset(dta.sch,select=c("SCHID","PUBLICSCH","PRIVATESCH",
"CATHOLICSCH","PLACESIZESCH","PCTSPANISHSCH","PCTBLACKSCH","LEASTPROUD.mean"))

sum(dta.full.A$SCHID %in% dta.sch.sub$SCHID)

#dta.full.B <- as.data.frame(dta.full.A)

length(colnames(dta.full.A))
length(unique(colnames(dta.full.A)))

length(colnames(dta.sch.sub))
length(unique(colnames(dta.sch.sub)))

sum(colnames(dta.sch.sub) %in% colnames(dta.full.A))
which(colnames(dta.sch.sub) %in% colnames(dta.full.A))

dta.full.1 <- merge(dta.full.A,dta.sch.sub,by="SCHID")

cn <- colnames(dta.full.1)
cn2 <- substr(cn,start=3,stop=30)
colnames(dta.full.1) <- cn2
dta.full.1$STUID <- dta.full.1$v5

### SR=self-report
### PR=partner report
dta.full.1$FTWHTBLKDADSR <- NA
dta.full.1$FTWHTBLKMOMSR <- NA
dta.full.1$FTWHTBLKDADPR <- NA
dta.full.1$FTWHTBLKMOMPR <- NA

dta.full.1$FTBLKSDADSR <- NA
dta.full.1$FTBLKSMOMSR <- NA
dta.full.1$FTBLKSDADPR <- NA
dta.full.1$FTBLKSMOMPR <- NA

dta.full.1$PIDDAD65SR <- NA
dta.full.1$PIDMOM65SR <- NA
dta.full.1$PIDDAD65PR <- NA
dta.full.1$PIDMOM65PR <- NA

dta.full.1$FTWHTSSTU <- NA
dta.full.1$FTBLKSSTU <- NA

cor(dta.par$FTBLKSSTU,dta.par$FTBLKSPAR,use="pairwise.complete.obs")

dta.full.1$FTBLKS65 <- NA#as.numeric(as.character(dta.full$v261r))
dta.full.1$FTBLKS65[dta.full.1$v261r=="15"] <- 15
dta.full.1$FTBLKS65[dta.full.1$v261r=="30"] <- 30
dta.full.1$FTBLKS65[dta.full.1$v261r=="40"] <- 40
dta.full.1$FTBLKS65[dta.full.1$v261r=="45"] <- 45
dta.full.1$FTBLKS65[dta.full.1$v261r=="50"] <- 50
dta.full.1$FTBLKS65[dta.full.1$v261r=="55"] <- 55
dta.full.1$FTBLKS65[dta.full.1$v261r=="60"] <- 60
dta.full.1$FTBLKS65[dta.full.1$v261r=="66"] <- 66
dta.full.1$FTBLKS65[dta.full.1$v261r=="70"] <- 70
dta.full.1$FTBLKS65[dta.full.1$v261r=="75"] <- 75
dta.full.1$FTBLKS65[dta.full.1$v261r=="80"] <- 80
dta.full.1$FTBLKS65[dta.full.1$v261r=="83"] <- 83
dta.full.1$FTBLKS65[dta.full.1$v261r=="85"] <- 85
dta.full.1$FTBLKS65[dta.full.1$v261r=="90"] <- 90
dta.full.1$FTBLKS65[dta.full.1$v261r=="92"] <- 92
dta.full.1$FTBLKS65[dta.full.1$v261r=="93"] <- 93
dta.full.1$FTBLKS65[dta.full.1$v261r=="95"] <- 95
#dta.full.1$FTBLKS65[dta.full.1$v261r==""] <-
dta.full.1$FTBLKS65[dta.full.1$v261r=="97-100 DEGREES"] <- 98
dta.full.1$FTBLKS65[dta.full.1$v261r=="ZERO DEGREES"] <- 0

### add parental data via loop
for(i in 1:dim(dta.full.1)[1]){
	id <- dta.full.1$STUID[i]
	idx <- which(dta.par$STUID==id)
	if(length(idx)==1){
		dad <- dta.par$FATHER[idx]==1
		mom <- dta.par$MOTHER[idx]==1
		if(mom){
			dta.full.1$FTWHTSSTU[i] <- dta.par$FTWHTSSTU[idx]
			dta.full.1$FTBLKSSTU[i] <- dta.par$FTBLKSSTU[idx]

			dta.full.1$FTBLKSDADSR[i] <- NA
			dta.full.1$FTBLKSMOMSR[i] <- dta.par$FTBLKSPAR[idx]

			dta.full.1$FTBLKSDADPR[i] <- dta.par$FTBLKSSPO[idx]
			dta.full.1$FTBLKSMOMPR[i] <- NA
			
			dta.full.1$PIDDADSR65[i] <- NA
			dta.full.1$PIDMOMSR65[i] <- dta.par$PARPID[idx]

			dta.full.1$PIDDADPR65[i] <- dta.par$PARPIDSPO[idx]
			dta.full.1$PIDMOMPR65[i] <- NA
			
			dta.full.1$FTWHTBLKDADSR[i] <- NA
			dta.full.1$FTWHTBLKMOMSR[i] <- dta.par$FTWHTSPAR[idx]-dta.par$FTBLKSPAR[idx]

			dta.full.1$FTWHTBLKDADPR[i] <- dta.par$FTWHTSSPO[idx]-dta.par$FTBLKSSPO[idx]
			dta.full.1$FTWHTBLKMOMPR[i] <- NA 
		}
		if(dad){
			dta.full.1$FTWHTSSTU[i] <- dta.par$FTWHTSSTU[idx]
			dta.full.1$FTBLKSSTU[i] <- dta.par$FTBLKSSTU[idx]

			dta.full.1$FTBLKSDADSR[i] <- dta.par$FTBLKSPAR[idx]
			dta.full.1$FTBLKSMOMSR[i] <- NA

			dta.full.1$FTBLKSDADPR[i] <- NA
			dta.full.1$FTBLKSMOMPR[i] <- dta.par$FTBLKSSPO[idx]
			
			dta.full.1$PIDDADSR65[i] <- dta.par$PARPID[idx]
			dta.full.1$PIDMOMSR65[i] <- NA

			dta.full.1$PIDDADPR65[i] <- NA
			dta.full.1$PIDMOMPR65[i] <- dta.par$PARPIDSPO[idx]

			dta.full.1$FTWHTBLKDADSR[i] <- dta.par$FTWHTSPAR[idx]-dta.par$FTBLKSPAR[idx]
			dta.full.1$FTWHTBLKMOMSR[i] <- NA

			dta.full.1$FTWHTBLKDADPR[i] <- NA 
			dta.full.1$FTWHTBLKMOMPR[i] <- dta.par$FTWHTSSPO[idx]-dta.par$FTBLKSSPO[idx] 

		}
	}
	if(length(idx)==2){
		dta.full.1$FTWHTSSTU[i] <- dta.par$FTWHTSSTU[idx[1]]
		dta.full.1$FTBLKSSTU[i] <- dta.par$FTBLKSSTU[idx[1]]

		#cat("Here at ",i,"\n")
		parent1 <- idx[1]
		parent2 <- idx[2]
		dad1 <- dta.par$FATHER[parent1]==1
		mom1 <- dta.par$MOTHER[parent1]==1
		if(dad1){
			dad.idx <- parent1
			mom.idx <- parent2
		}
		if(mom1){
			dad.idx <- parent2
			mom.idx <- parent1
		}
		dta.full.1$FTBLKSDADSR[i] <- dta.par$FTBLKSPAR[dad.idx]
		dta.full.1$FTBLKSMOMSR[i] <- dta.par$FTBLKSPAR[mom.idx]

		dta.full.1$FTBLKSDADPR[i] <- dta.par$FTBLKSSPO[mom.idx]
		dta.full.1$FTBLKSMOMPR[i] <- dta.par$FTBLKSSPO[dad.idx]
			
		dta.full.1$PIDDADSR65[i] <- dta.par$PARPID[dad.idx]
		dta.full.1$PIDMOMSR65[i] <- dta.par$PARPID[mom.idx]

		dta.full.1$PIDDADPR65[i] <- dta.par$PARPIDSPO[mom.idx]
		dta.full.1$PIDMOMPR65[i] <- dta.par$PARPIDSPO[dad.idx]
	
		dta.full.1$FTWHTBLKDADSR[i] <- dta.par$FTWHTSPAR[dad.idx]-dta.par$FTBLKSPAR[dad.idx]
		dta.full.1$FTWHTBLKMOMSR[i] <- dta.par$FTWHTSPAR[mom.idx]-dta.par$FTBLKSPAR[mom.idx]

		dta.full.1$FTWHTBLKDADPR[i] <- dta.par$FTWHTSSPO[dad.idx]-dta.par$FTBLKSSPO[dad.idx] 
		dta.full.1$FTWHTBLKMOMPR[i] <- dta.par$FTWHTSSPO[mom.idx]-dta.par$FTBLKSSPO[mom.idx] 
	}
	if(length(idx)> 2){
		cat("Here at ",i,"\n")
	}
	cat("From parental data: ",dta.full.1$FTBLKSSTU[i],"\n")
	cat("From other data data: ",dta.full.1$FTBLKS65[i],"\n")

}

dta.full.2 <- dta.full.1

##### LOAD COUNTY DATA
load("/YPSPS/countydata052317.Rdata")

dta.full.2$ZIP65 <- as.numeric(as.character(dta.full.2$v5013)) 
length(unique(counties65demos2$ZIP))
colnames(counties65demos2)[2] <- "ZIP65"
dta65h <- merge(dta.full.2,counties65demos2,by="ZIP65",all.x=T)
cn <- colnames(dta65h)
cn2 <- substr(cn,start=3,stop=31)
colnames(dta65h) <- cn2

counties82demos2sub <- subset(counties82demos2,select=c("ZIP","COPCNHBL","COPCIMM","COPCBCH","COUNEMP","STCOFIPS"))
length(unique(counties82demos2$ZIP))

colnames(counties82demos2sub)[2:6] <- paste(c("COPCNHBL","COPCIMM","COPCBCH","COUNEMP","STCOFIPS"),".82",sep="")
colnames(counties82demos2sub)[1] <- "ZIP82"

dta65h$ZIP82 <- as.numeric(as.character(dta65h$v5011))

#### FIX MERGE (multiple ZIPs matching)
dta65i <- merge(dta65h,counties82demos2sub,by="ZIP82",all.x=T)
cn <- colnames(dta65i)
cn2 <- substr(cn,start=3,stop=31)
colnames(dta65i) <- cn2

######
counties97demos2sub <- subset(counties97demos2,select=c("ZIP","COPCNHBL.y","COPCIMM.y","COPCBCH.y","COUNEMP.y","STCOFIPS"))
colnames(counties97demos2sub)[2:6] <- paste(c("COPCNHBL","COPCIMM","COPCBCH","COUNEMP","STCOFIPS"),".97",sep="")
colnames(counties97demos2sub)[1] <- "ZIP97"

dta65i$ZIP97 <- as.numeric(as.character(dta65i$v5010))
dta65 <- merge(dta65i,counties97demos2sub,by="ZIP97",all.x=T)
cn <- colnames(dta65)
cn2 <- substr(cn,start=3,stop=31)
colnames(dta65) <- cn2

######

dta65$ATTENDCOL6573 <- 1*(dta65$v648=="R ATTENDED COLLEGE" & ! dta$v648 %in% c(NA))
dta65$ATTENDCOL7382 <- 1*(dta65$v2023=="R ATTENDED COLLEGE SINCE 1973" & ! dta$v2023 %in% c(NA))

dta65$COLLEGE6573 <- 1*(dta65$v651=="YES" & ! dta65$v651 %in% c(NA))
dta65$COLLEGE7382 <- 1*(dta65$v2025=="YES" & ! dta65$v2025 %in% c(NA))

dta65$ATTENDCOL <- 1*(dta65$ATTENDCOL6573==1 | dta65$ATTENDCOL7382==1)
dta65$COLLEGEGRAD <- 1*(dta65$COLLEGE7382==1 | dta65$COLLEGE6573==1)

### state = v223

dta65$FTWHTS65 <- NA#as.numeric(as.character(dta.full$v259r))

dta65$FTWHTS65[dta65$v259r=="15"] <- 15
dta65$FTWHTS65[dta65$v259r=="30"] <- 30
dta65$FTWHTS65[dta65$v259r=="40"] <- 40
dta65$FTWHTS65[dta65$v259r=="45"] <- 45
dta65$FTWHTS65[dta65$v259r=="50"] <- 50
dta65$FTWHTS65[dta65$v259r=="55"] <- 55
dta65$FTWHTS65[dta65$v259r=="60"] <- 60
dta65$FTWHTS65[dta65$v259r=="66"] <- 66
dta65$FTWHTS65[dta65$v259r=="70"] <- 70
dta65$FTWHTS65[dta65$v259r=="75"] <- 75
dta65$FTWHTS65[dta65$v259r=="80"] <- 80
dta65$FTWHTS65[dta65$v259r=="83"] <- 83
dta65$FTWHTS65[dta65$v259r=="85"] <- 85
dta65$FTWHTS65[dta65$v259r=="90"] <- 90
dta65$FTWHTS65[dta65$v259r=="92"] <- 92
dta65$FTWHTS65[dta65$v259r=="93"] <- 93
dta65$FTWHTS65[dta65$v259r=="95"] <- 95
#dta65$FTWHTS65[dta65$v259r==""] <-
dta65$FTWHTS65[dta65$v259r=="97-100 DEGREES"] <- 98
dta65$FTWHTS65[dta65$v259r=="ZERO DEGREES"] <- 0

dta65$FTBLKS65 <- NA#as.numeric(as.character(dta.full$v261r))
dta65$FTBLKS65[dta65$v261r=="15"] <- 15
dta65$FTBLKS65[dta65$v261r=="30"] <- 30
dta65$FTBLKS65[dta65$v261r=="40"] <- 40
dta65$FTBLKS65[dta65$v261r=="45"] <- 45
dta65$FTBLKS65[dta65$v261r=="50"] <- 50
dta65$FTBLKS65[dta65$v261r=="55"] <- 55
dta65$FTBLKS65[dta65$v261r=="60"] <- 60
dta65$FTBLKS65[dta65$v261r=="66"] <- 66
dta65$FTBLKS65[dta65$v261r=="70"] <- 70
dta65$FTBLKS65[dta65$v261r=="75"] <- 75
dta65$FTBLKS65[dta65$v261r=="80"] <- 80
dta65$FTBLKS65[dta65$v261r=="83"] <- 83
dta65$FTBLKS65[dta65$v261r=="85"] <- 85
dta65$FTBLKS65[dta65$v261r=="90"] <- 90
dta65$FTBLKS65[dta65$v261r=="92"] <- 92
dta65$FTBLKS65[dta65$v261r=="93"] <- 93
dta65$FTBLKS65[dta65$v261r=="95"] <- 95
#dta65$FTBLKS65[dta65$v261r==""] <-
dta65$FTBLKS65[dta65$v261r=="97-100 DEGREES"] <- 98
dta65$FTBLKS65[dta65$v261r=="ZERO DEGREES"] <- 0

dta65$FTWHTS73 <- NA
dta65$FTWHTS73[dta65$v437r=="0"] <- 0
dta65$FTWHTS73[dta65$v437r=="10"] <- 10
dta65$FTWHTS73[dta65$v437r=="15"] <- 15
dta65$FTWHTS73[dta65$v437r=="20"] <- 20
dta65$FTWHTS73[dta65$v437r=="30"] <- 30
dta65$FTWHTS73[dta65$v437r=="40"] <- 40
dta65$FTWHTS73[dta65$v437r=="45"] <- 45
dta65$FTWHTS73[dta65$v437r=="46"] <- 46
dta65$FTWHTS73[dta65$v437r=="50"] <- 50
dta65$FTWHTS73[dta65$v437r=="55"] <- 55
dta65$FTWHTS73[dta65$v437r=="60"] <- 60
dta65$FTWHTS73[dta65$v437r=="66"] <- 66
dta65$FTWHTS73[dta65$v437r=="70"] <- 70
dta65$FTWHTS73[dta65$v437r=="75"] <- 75
dta65$FTWHTS73[dta65$v437r=="80"] <- 80
dta65$FTWHTS73[dta65$v437r=="83"] <- 83
dta65$FTWHTS73[dta65$v437r=="85"] <- 85
dta65$FTWHTS73[dta65$v437r=="88"] <- 88
dta65$FTWHTS73[dta65$v437r=="90"] <- 90
dta65$FTWHTS73[dta65$v437r=="92"] <- 92
dta65$FTWHTS73[dta65$v437r=="93"] <- 93
dta65$FTWHTS73[dta65$v437r=="95"] <- 95
dta65$FTWHTS73[dta65$v437r=="50 DEGREES. NO FEELING TOWARDS GROUP"] <- 50
dta65$FTWHTS73[dta65$v437r=="100"] <- 100
dta65$FTWHTS73[dta65$v437r=="96 'DEGREES'"] <- 96
dta65$FTWHTS73[dta65$v437r=="97-100 DEGREES"] <- 98
dta65$FTWHTS73[dta65$v437r=="0 'DEGREES'"] <- 0
dta65$FTWHTS73[dta65$v437r=="49 'DEGREES'"] <- 49

dta65$FTBLKS73 <- NA
dta65$FTBLKS73[dta65$v439r=="0"] <- 0
dta65$FTBLKS73[dta65$v439r=="10"] <- 10
dta65$FTBLKS73[dta65$v439r=="15"] <- 15
dta65$FTBLKS73[dta65$v439r=="20"] <- 20
dta65$FTBLKS73[dta65$v439r=="30"] <- 30
dta65$FTBLKS73[dta65$v439r=="40"] <- 40
dta65$FTBLKS73[dta65$v439r=="45"] <- 45
dta65$FTBLKS73[dta65$v439r=="46"] <- 46
dta65$FTBLKS73[dta65$v439r=="50"] <- 50
dta65$FTBLKS73[dta65$v439r=="55"] <- 55
dta65$FTBLKS73[dta65$v439r=="60"] <- 60
dta65$FTBLKS73[dta65$v439r=="65"] <- 65
dta65$FTBLKS73[dta65$v439r=="66"] <- 66
dta65$FTBLKS73[dta65$v439r=="70"] <- 70
dta65$FTBLKS73[dta65$v439r=="75"] <- 75
dta65$FTBLKS73[dta65$v439r=="80"] <- 80
dta65$FTBLKS73[dta65$v439r=="83"] <- 83
dta65$FTBLKS73[dta65$v439r=="85"] <- 85
dta65$FTBLKS73[dta65$v439r=="88"] <- 88
dta65$FTBLKS73[dta65$v439r=="90"] <- 90
dta65$FTBLKS73[dta65$v439r=="92"] <- 92
dta65$FTBLKS73[dta65$v439r=="93"] <- 93
dta65$FTBLKS73[dta65$v439r=="95"] <- 95
dta65$FTBLKS73[dta65$v439r=="50 DEGREES. NO FEELING TOWARDS GROUP"] <- 50
dta65$FTBLKS73[dta65$v439r=="100"] <- 100
dta65$FTBLKS73[dta65$v439r=="96 'DEGREES'"] <- 96
dta65$FTBLKS73[dta65$v439r=="97-100 DEGREES"] <- 98
dta65$FTBLKS73[dta65$v439r=="0 'DEGREES'"] <- 0
dta65$FTBLKS73[dta65$v439r=="49 'DEGREES'"] <- 49

dta65$FTBLKS82 <- NA
dta65$FTBLKS82[dta65$v1527r=="10"] <- 10
dta65$FTBLKS82[dta65$v1527r=="15"] <- 15
dta65$FTBLKS82[dta65$v1527r=="20"] <- 20
dta65$FTBLKS82[dta65$v1527r=="30"] <- 30
dta65$FTBLKS82[dta65$v1527r=="40"] <- 40
dta65$FTBLKS82[dta65$v1527r=="45"] <- 45
dta65$FTBLKS82[dta65$v1527r=="46"] <- 46
dta65$FTBLKS82[dta65$v1527r=="50"] <- 50
dta65$FTBLKS82[dta65$v1527r=="55"] <- 55
dta65$FTBLKS82[dta65$v1527r=="60"] <- 60
dta65$FTBLKS82[dta65$v1527r=="65"] <- 65
dta65$FTBLKS82[dta65$v1527r=="66"] <- 66
dta65$FTBLKS82[dta65$v1527r=="70"] <- 70
dta65$FTBLKS82[dta65$v1527r=="75"] <- 75
dta65$FTBLKS82[dta65$v1527r=="80"] <- 80
dta65$FTBLKS82[dta65$v1527r=="83"] <- 83
dta65$FTBLKS82[dta65$v1527r=="85"] <- 85
dta65$FTBLKS82[dta65$v1527r=="88"] <- 88
dta65$FTBLKS82[dta65$v1527r=="90"] <- 90
dta65$FTBLKS82[dta65$v1527r=="92"] <- 92
dta65$FTBLKS82[dta65$v1527r=="93"] <- 93
dta65$FTBLKS82[dta65$v1527r=="95"] <- 95
dta65$FTBLKS82[dta65$v1527r=="50 'DEGREES', NO FEELING TOWARDS GROUP"] <- 50
dta65$FTBLKS82[dta65$v1527r=="100"] <- 100
dta65$FTBLKS82[dta65$v1527r=="97-100 'DEGREES'"] <- 98
dta65$FTBLKS82[dta65$v1527r=="ZERO DEGREES"] <- 0
dta65$FTBLKS82[dta65$v1527r=="49 'DEGREES'"] <- 49

dta65$FTWHTS82 <- NA
dta65$FTWHTS82[dta65$v1524r=="10"] <- 10
dta65$FTWHTS82[dta65$v1524r=="15"] <- 15
dta65$FTWHTS82[dta65$v1524r=="20"] <- 20
dta65$FTWHTS82[dta65$v1524r=="30"] <- 30
dta65$FTWHTS82[dta65$v1524r=="40"] <- 40
dta65$FTWHTS82[dta65$v1524r=="45"] <- 45
dta65$FTWHTS82[dta65$v1524r=="46"] <- 46
dta65$FTWHTS82[dta65$v1524r=="50"] <- 50
dta65$FTWHTS82[dta65$v1524r=="55"] <- 55
dta65$FTWHTS82[dta65$v1524r=="60"] <- 60
dta65$FTWHTS82[dta65$v1524r=="65"] <- 65
dta65$FTWHTS82[dta65$v1524r=="66"] <- 66
dta65$FTWHTS82[dta65$v1524r=="70"] <- 70
dta65$FTWHTS82[dta65$v1524r=="75"] <- 75
dta65$FTWHTS82[dta65$v1524r=="80"] <- 80
dta65$FTWHTS82[dta65$v1524r=="83"] <- 83
dta65$FTWHTS82[dta65$v1524r=="85"] <- 85
dta65$FTWHTS82[dta65$v1524r=="88"] <- 88
dta65$FTWHTS82[dta65$v1524r=="90"] <- 90
dta65$FTWHTS82[dta65$v1524r=="92"] <- 92
dta65$FTWHTS82[dta65$v1524r=="93"] <- 93
dta65$FTWHTS82[dta65$v1524r=="95"] <- 95
dta65$FTWHTS82[dta65$v1524r=="50 'DEGREES', NO FEELING TOWARDS GROUP"] <- 50
dta65$FTWHTS82[dta65$v1524r=="100"] <- 100
dta65$FTWHTS82[dta65$v1524r=="96 'DEGREES'"] <- 96
dta65$FTWHTS82[dta65$v1524r=="97-100 'DEGREES'"] <- 98
dta65$FTWHTS82[dta65$v1524r=="0 'DEGREES'"] <- 0
dta65$FTWHTS82[dta65$v1524r=="49 'DEGREES'"] <- 49

dta65$FTBLKS97 <- as.numeric(as.character(dta65$v5611r))
dta65$FTBLKS97[dta65$v5611r=="NO FEELING TOWARDS GROUP"] <- 50

### ftwhites
dta65$FTWHTS97 <- as.numeric(as.character(dta65$v1524r))
dta65$FTWHTS97[dta65$v1524r=="0 'DEGREES'"] <- 0
dta65$FTWHTS97[dta65$v1524r=="50 'DEGREES', NO FEELING TOWARDS GROUP"] <- 50
dta65$FTWHTS97[dta65$v1524r=="96 'DEGREES'"] <- 96
dta65$FTWHTS97[dta65$v1524r=="97-100 'DEGREES'"] <- 98

#### HELP BLKS

dta65$HLPBLKS73 <- NA
dta65$HLPBLKS73[dta65$v344r=="GOVERNMENT SHOULD HELP MINORITY GROUPS"] <- 1
dta65$HLPBLKS73[dta65$v344r=="2"] <- 2
dta65$HLPBLKS73[dta65$v344r=="3"] <- 3
dta65$HLPBLKS73[dta65$v344r=="4"] <- 4
dta65$HLPBLKS73[dta65$v344r=="5"] <- 5
dta65$HLPBLKS73[dta65$v344r=="6"] <- 6
dta65$HLPBLKS73[dta65$v344r=="MINORITY GROUPS SHOULD HELP THEMSELVES"] <- 7

dta65$HLPBLKS82 <- NA
dta65$HLPBLKS82[dta65$v1233r=="GOVERNMENT SHOULD HELP MINORITY GROUPS"] <- 1
dta65$HLPBLKS82[dta65$v1233r=="2"] <- 2
dta65$HLPBLKS82[dta65$v1233r=="3"] <- 3
dta65$HLPBLKS82[dta65$v1233r=="4"] <- 4
dta65$HLPBLKS82[dta65$v1233r=="5"] <- 5
dta65$HLPBLKS82[dta65$v1233r=="6"] <- 6
dta65$HLPBLKS82[dta65$v1233r=="MINORITY GROUPS SHOULD HELP THEMSELVES"] <- 7

dta65$HLPBLKS97 <- NA
dta65$HLPBLKS97[dta65$v5252r=="GOVT SHOULD HELP MINORITY GROUPS"] <- 1
dta65$HLPBLKS97[dta65$v5252r=="2"] <- 2
dta65$HLPBLKS97[dta65$v5252r=="3"] <- 3
dta65$HLPBLKS97[dta65$v5252r=="4"] <- 4
dta65$HLPBLKS97[dta65$v5252r=="5"] <- 5
dta65$HLPBLKS97[dta65$v5252r=="6"] <- 6
dta65$HLPBLKS97[dta65$v5252r=="MINORITY GROUPS SHOULD HELP THEMSELVES"] <- 7

dta65$FEDYEARS <- NA
dta65$FEDYEARS[dta65$v207=="NONE" & ! dta65$v207 %in% c(NA)] <- 0
dta65$FEDYEARS[dta65$v207=="1 GRADE" & ! dta65$v207 %in% c(NA)] <- 1
dta65$FEDYEARS[dta65$v207=="2 GRADES" & ! dta65$v207 %in% c(NA)] <- 2
dta65$FEDYEARS[dta65$v207=="3 GRADES" & ! dta65$v207 %in% c(NA)] <- 3
dta65$FEDYEARS[dta65$v207=="4 GRADES" & ! dta65$v207 %in% c(NA)] <- 4
dta65$FEDYEARS[dta65$v207=="5 GRADES - (OR 1ST TO 8TH GRADE, NA EXACT LEVEL COMPLETED)" & ! dta65$v207 %in% c(NA)] <- 5
dta65$FEDYEARS[dta65$v207=="6 GRADES" & ! dta65$v207 %in% c(NA)] <- 6
dta65$FEDYEARS[dta65$v207=="7 GRADES" & ! dta65$v207 %in% c(NA)] <- 7
dta65$FEDYEARS[dta65$v207=="8 GRADES" & ! dta65$v207 %in% c(NA)] <- 8
dta65$FEDYEARS[dta65$v207=="9 GRADES" & ! dta65$v207 %in% c(NA)] <- 9
dta65$FEDYEARS[dta65$v207=="10 GRADES" & ! dta65$v207 %in% c(NA)] <- 10
dta65$FEDYEARS[dta65$v207=="11 GRADES - (OR 9TH TO 12TH GRADE, NA EXACT LEVEL COMPLETED)" & ! dta65$v207 %in% c(NA)] <- 11
dta65$FEDYEARS[dta65$v207=="HIGH SCHOOL (NA WHETHER GRADUATED)" & ! dta65$v207 %in% c(NA)] <- 11
dta65$FEDYEARS[dta65$v207=="9 GRADES PLUS NON-COLLEGE TRAINING" & ! dta65$v207 %in% c(NA)] <- 10
dta65$FEDYEARS[dta65$v207=="10 GRADES PLUS NON-COLLEGE TRAINING" & ! dta65$v207 %in% c(NA)] <- 11
dta65$FEDYEARS[dta65$v207=="11 GRADES PLUS NON-COLLEGE TRAINING" & ! dta65$v207 %in% c(NA)] <- 12
dta65$FEDYEARS[dta65$v207=="12 GRADES" & ! dta65$v207 %in% c(NA)] <- 12
dta65$FEDYEARS[dta65$v207=="12 GRADES PLUS NON-COLLEGE TRAINING" & ! dta65$v207 %in% c(NA)] <- 13
dta65$FEDYEARS[dta65$v207=="SOME COLLEGE" & ! dta65$v207 %in% c(NA)] <- 14
dta65$FEDYEARS[dta65$v207=="COLLEGE (NA WHETHER GRADUATED)" & ! dta65$v207 %in% c(NA)] <- 14
dta65$FEDYEARS[dta65$v207=="BACHELOR'S DEGREE (4 YEARS COLLEGE) - BS, BA, AB, AB IN TH," & ! dta65$v207 %in% c(NA)] <- 16
dta65$FEDYEARS[dta65$v207=="MASTER'S DEGREE - MS, MSC, MA, MAT, MBA, MDS, MED MFA, EDM," & ! dta65$v207 %in% c(NA)] <- 18
dta65$FEDYEARS[dta65$v207=="PHD, LITD, SCD, LHD, DFA, DLIT, DPH, DPHIL, DSC" & ! dta65$v207 %in% c(NA)] <- 20
dta65$FEDYEARS[dta65$v207=="LLD, JD, JSD, SJD" & ! dta65$v207 %in% c(NA)] <- 19
dta65$FEDYEARS[dta65$v207=="MD, DDS, DVM, VS" & ! dta65$v207 %in% c(NA)] <- 20

dta65$CATHOLIC65 <- 1*(dta65$v200=="ROMAN CATHOLIC")
dta65$JEWISH65 <- 1*(dta65$v200=="JEWISH")
dta65$SOUTH65 <- 1*(dta65$v224 %in% c("VIRGINIA","ARKANSAS","ALABAMA","GEORGIA","FLORIDA","MISSISSIPPI","NORTH CAROLINA","SOUTH CAROLINA","TEXAS","TENNESSEE"))

dta65$WHITE <- 1*(dta65$v232=="WHITE")
dta65$BLACK <- 1*(dta65$v232=="NEGRO")
dta65$MALE <- 1*(dta65$v6001=="MALE")
dta65$AGE65 <- 1965-as.numeric(as.character(dta65$v222))

dta65$SAMESTATE6582 <- 0
dta65$SAMESTATE6582[dta65$v5007==dta65$v5009] <- 1

dta65$SAMECOUNTY6582 <- 0
dta65$SAMECOUNTY6582[dta65$STCOFIPS==dta65$STCOFIPS.82 & ! dta65$STCOFIPS %in% c(NA) & ! dta65$STCOFIPS.82 %in% c(NA)] <- 1

dta65$SAMECOUNTY6597 <- 0
dta65$SAMECOUNTY6597[dta65$STCOFIPS==dta65$STCOFIPS.97 & ! dta65$STCOFIPS %in% c(NA) & ! dta65$STCOFIPS.97 %in% c(NA)] <- 1

dta65$FTDIF97 <- dta65$FTBLKS97-dta65$FTWHTS97
dta65$FTDIF82 <- dta65$FTBLKS82-dta65$FTWHTS82
dta65$FTDIF73 <- dta65$FTBLKS73-dta65$FTWHTS73
dta65$FTDIF65 <- dta65$FTBLKS65-dta65$FTWHTS65

dta65$PID65 <- NA
dta65$PID65[dta65$v84=="STRONG DEMOCRAT"] <- 1
dta65$PID65[dta65$v84=="NOT VERY STRONG DEMOCRAT"] <- 2
dta65$PID65[dta65$v84=="YES, DEMOCRAT"] <- 3
dta65$PID65[dta65$v84=="NO, NEITHER"] <- 4
dta65$PID65[dta65$v84=="YES, REPUBLICAN"] <- 5
dta65$PID65[dta65$v84=="NOT VERY STRONG REPUBLICAN"] <- 6
dta65$PID65[dta65$v84=="STRONG REPUBLICAN"] <- 7

dta65$PID82 <- NA
dta65$PID82[dta65$v1608=="STRONG DEMOCRAT (510)"] <- 1
dta65$PID82[dta65$v1608=="WEAK DEMOCRAT (550, 580, 590)"] <- 2
dta65$PID82[dta65$v1608=="INDEPENDENT-DEMOCRAT (205, 305, 405)"] <- 3
dta65$PID82[dta65$v1608=="INDEPENDENT-INDEPENDENT (203, 208, 209)"] <- 4
dta65$PID82[dta65$v1608=="INDEPENDENT-REPUBLICAN (201, 301, 401)"] <- 5
dta65$PID82[dta65$v1608=="WEAK REPUBLICAN (150, 180, 190)"] <- 6
dta65$PID82[dta65$v1608=="STRONG REPUBLICAN (110)"] <- 7

dta65$PID97 <- NA
dta65$PID97[dta65$v5754=="STRONG DEMOCRAT"] <- 1
dta65$PID97[dta65$v5754=="WEAK DEMOCRAT"] <- 2
dta65$PID97[dta65$v5754=="INDEPENDENT-DEMOCRAT"] <- 3
dta65$PID97[dta65$v5754=="INDEPENDENT-INDEPENDENT"] <- 4
dta65$PID97[dta65$v5754=="INDEPENDENT-REPUBLICAN"] <- 5
dta65$PID97[dta65$v5754=="WEAK REPUBLICAN"] <- 6
dta65$PID97[dta65$v5754=="STRONG REPUBLICAN"] <- 7

### parents
dta65$FTBLKSPAR <- NA
dta65$FTBLKSPAR[! dta65$FTBLKSMOMSR %in% c(NA) & dta65$FTBLKSDADSR %in% c(NA)] <- dta65$FTBLKSMOMSR[! dta65$FTBLKSMOMSR %in% c(NA) & dta65$FTBLKSDADSR %in% c(NA)]
dta65$FTBLKSPAR[dta65$FTBLKSMOMSR %in% c(NA) & ! dta65$FTBLKSDADSR %in% c(NA)] <- dta65$FTBLKSDADSR[dta65$FTBLKSMOMSR %in% c(NA) & ! dta65$FTBLKSDADSR %in% c(NA)]

dta65$FTBLKSJOINT <- apply(cbind(dta65$FTBLKSDADSR,dta65$FTBLKSMOMSR),1,mean)
dta65$FTBLKSPAR[! dta65$FTBLKSMOMSR %in% c(NA) & ! dta65$FTBLKSDADSR %in% c(NA)] <- dta65$FTBLKSJOINT[! dta65$FTBLKSMOMSR %in% c(NA) & ! dta65$FTBLKSDADSR %in% c(NA)]

dta65$FTWHTBLKPAR <- NA
dta65$FTWHTBLKPAR[! dta65$FTWHTBLKMOMSR %in% c(NA) & dta65$FTWHTBLKDADSR %in% c(NA)] <- dta65$FTWHTBLKMOMSR[! dta65$FTWHTBLKMOMSR %in% c(NA) & dta65$FTWHTBLKDADSR %in% c(NA)]
dta65$FTWHTBLKPAR[dta65$FTWHTBLKMOMSR %in% c(NA) & ! dta65$FTWHTBLKDADSR %in% c(NA)] <- dta65$FTWHTBLKDADSR[dta65$FTWHTBLKMOMSR %in% c(NA) & ! dta65$FTWHTBLKDADSR %in% c(NA)]

dta65$FTWHTBLKJOINT <- apply(cbind(dta65$FTWHTBLKDADSR,dta65$FTWHTBLKMOMSR),1,mean)
dta65$FTWHTBLKPAR[! dta65$FTWHTBLKMOMSR %in% c(NA) & ! dta65$FTWHTBLKDADSR %in% c(NA)] <- dta65$FTWHTBLKJOINT[! dta65$FTWHTBLKMOMSR %in% c(NA) & ! dta65$FTWHTBLKDADSR %in% c(NA)]

dta65w <- dta65[dta65$BLACK==0,]

corrmat <- cor(cbind(dta65w$FTWHTBLKPAR,dta65w$LEASTPROUD.mean,dta65w$PCTBLACKSCH,dta65w$COPCNHBL.70),use="pairwise.complete.obs")
colnames(corrmat) <- rownames(corrmat) <- c("Parents","School Att","School Pct. Blk.","County Pct. Black")
xtable(corrmat)

dta65s <- as.data.frame(dta65[,-c(7:37)])
dta65sw <- as.data.frame(dta65w[,-c(7:37)])

######
######
### summarize YPSPS data

vars <- unique(c("COPCNHBL.70","COPCBCH.70","COUNEMP.70","COPCNHBL.82","COPCBCH.82","COUNEMP.82","COPCNHBL.70","COPCNHBL.97","COPCBCH.97","COUNEMP.97","LEASTPROUD.mean","FTWHTBLKPAR","PCTBLACKSCH","ATTENDCOL","COLLEGEGRAD","PID65","MALE","BLACK","AGE65","FEDYEARS","CATHOLIC65","JEWISH65","ATTENDCOL","LEASTPROUD.mean","FTWHTBLKPAR","PCTBLACKSCH","ATTENDCOL","COLLEGEGRAD","PID65","MALE","BLACK","AGE65","FEDYEARS","CATHOLIC65","JEWISH65","ATTENDCOL","COLLEGEGRAD"))
rmat <- matrix(NA,length(vars),4)
for(i in 1:length(vars)){

	txt <- paste("hold <- dta65sw$",vars[i],sep="")
	eval(parse(text=txt))

	rmat[i,1] <- min(hold,na.rm=T)
	rmat[i,2] <- max(hold,na.rm=T)
	rmat[i,3] <- mean(hold,na.rm=T)
	rmat[i,4] <- sd(hold,na.rm=T)
}
rownames(rmat) <- vars
colnames(rmat) <- c("Min","Max","Mean","SD")
xtable(rmat,digits=c(0,2,2,2,2))

### table 1: comparing contemporary, adolescent contexts

lout65p1 <- lm(I(FTWHTS65-FTBLKS65) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout82p1 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.82+COPCBCH.82+COUNEMP.82+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout97p1 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.97+COPCBCH.97+COUNEMP.97+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

lout65mat1 <- summary(lout65p1)$coef
lout82mat1 <- summary(lout82p1)$coef
lout97mat1 <- summary(lout97p1)$coef

#lout65p2 <- lm(I(FTWHTS65-FTBLKS65) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout82p2 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout97p2 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

lout82p3 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.82+COPCBCH.82+COUNEMP.82+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout97p3 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.97+COPCBCH.97+COUNEMP.97+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)

mf82 <- model.frame(lout82p3)
mf97 <- model.frame(lout97p3)

colnames(mf82)[1:7] <- colnames(mf97)[1:7] <- c("CHANGE","COPCNHBL.70","COPCBCH.70","COUNEMP.70","COPCNHBLNOW","COPCBCHNOW","COUNEMPNOW")

mf82$WAVE <- 1
mf97$WAVE <- 2

identical(colnames(mf82),colnames(mf97))

mfjoint <- rbind(mf82,mf97)
library(lme4)

x1 <- rnorm(10000,mean=12.86443,sd= 5.26997)
2*(1-sum(x1>0)/length(x1))

quantile(dta65sw$COPCNHBL.70)

loutjoint2 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)
loutjoint <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)

x10 <- rnorm(10000,mean=12.86443,sd=5.26997) 

library(texreg)
texreg(list(loutjoint,lout82p2,lout97p2,lout82p3,lout97p3),stars=c(0.05),custom.model.names=c("Later Prejudice","Prej. 82","Prej. 97","Prej. 82","Prej. 97"))

### interact unemployment
loutjoint <- lmer(CHANGE ~ COPCNHBL.70*COUNEMPNOW+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)

###### table 2
###### including measures of environment

dta65sw$FTWHTBLK65P <- dta65sw$FTWHTS65-dta65sw$FTBLKS65
dta65sw$FTWHTBLK65P[dta65sw$FTWHTS65 < dta65sw$FTBLKS65] <- 0

dta65sw$FTWHTBLK82P <- dta65sw$FTWHTS82-dta65sw$FTBLKS82
dta65sw$FTWHTBLK82P[dta65sw$FTWHTS82 < dta65sw$FTBLKS82] <- 0

dta65sw$FTWHTBLK97P <- dta65sw$FTWHTS97-dta65sw$FTBLKS97
dta65sw$FTWHTBLK97P[dta65sw$FTWHTS97 < dta65sw$FTBLKS97] <- 0

lout82r1 <- lm(FTWHTBLK82P ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout97r1 <- lm(FTWHTBLK97P ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

lout82r2 <- lm(FTWHTBLK82P ~COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.82+COPCBCH.82+COUNEMP.82+LEASTPROUD.mean+FTWHTBLKPAR+PCTBLACKSCH+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data =dta65sw)
lout97r2 <- lm(FTWHTBLK97P ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.97+COPCBCH.97+COUNEMP.97+LEASTPROUD.mean+FTWHTBLKPAR+PCTBLACKSCH+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

mf82 <- model.frame(lout82r2)
mf97 <- model.frame(lout97r2)

colnames(mf82)[1:7] <- colnames(mf97)[1:7] <- c("PREJUDICE","COPCNHBL.70","COPCBCH.70","COUNEMP.70","COPCNHBLNOW","COPCBCHNOW","COUNEMPNOW")

mf82$WAVE <- 1
mf97$WAVE <- 2

mfjoint <- rbind(mf82,mf97)

loutjoint1 <- lmer(PREJUDICE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+LEASTPROUD.mean+I(FTWHTBLKPAR/100)+I(PCTBLACKSCH/100)+ATTENDCOL+COLLEGEGRAD+PID65+MALE+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD+(1|WAVE),data=mfjoint)
loutjoint2 <- lmer(PREJUDICE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+LEASTPROUD.mean+I(FTWHTBLKPAR/100)+I(PCTBLACKSCH/100)+ATTENDCOL+COLLEGEGRAD+PID65+MALE+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD+(1|WAVE),data=mfjoint)

lout65r2 <- lm(FTWHTBLK65P ~COPCNHBL.70+COPCBCH.70+COUNEMP.70+LEASTPROUD.mean+I(FTWHTBLKPAR/100)+I(PCTBLACKSCH/100)+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data =dta65sw)
lout82r2 <- lm(FTWHTBLK82P ~COPCNHBL.70+COPCBCH.70+COUNEMP.70+LEASTPROUD.mean+I(FTWHTBLKPAR/100)+I(PCTBLACKSCH/100)+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data =dta65sw)
lout97r2 <- lm(FTWHTBLK97P ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+LEASTPROUD.mean+I(FTWHTBLKPAR/100)+I(PCTBLACKSCH/100)+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

texreg(list(loutjoint1,loutjoint2,lout65r2,lout82r2,lout97r2),stars=c(0.05),custom.model.names=c("Prejudice","Prejudice","Prej. 65","Prej. 82","Prej. 97"))

x10 <- rnorm(10000,mean=-13.70,sd=8.44)
1-sum(x10 < 0)/length(x10)

quantile(dta65s$COPCNHBL.70,na.rm=T)
quantile(dta65s$FTWHTBLKPAR,na.rm=T)/100
quantile(dta65s$PCTBLACKSCH,na.rm=T)/100
quantile(dta65s$LEASTPROUD.mean,na.rm=T)

##### SAME COUNTY

dta65sw$SAMECOUNTY6582B <- 1*(dta65sw$SAMECOUNTY6582==1 & ! dta65sw$SAMECOUNTY6582 %in% c(NA))
dta65sw$SAMECOUNTY6597B <- 1*(dta65sw$SAMECOUNTY6597==1 & ! dta65sw$SAMECOUNTY6597 %in% c(NA))

lout82p3 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.82+COPCBCH.82+COUNEMP.82+SAMECOUNTY6582B+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout97p3 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.97+COPCBCH.97+COUNEMP.97+SAMECOUNTY6597B+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)

mf82 <- model.frame(lout82p3)
mf97 <- model.frame(lout97p3)

colnames(mf82)[1:8] <- colnames(mf97)[1:8] <- c("CHANGE","COPCNHBL.70","COPCBCH.70","COUNEMP.70","COPCNHBLNOW","COPCBCHNOW","COUNEMPNOW","SAMECOUNTY")

mf82$WAVE <- 1
mf97$WAVE <- 2

identical(colnames(mf82),colnames(mf97))

mfjoint <- rbind(mf82,mf97)
library(lme4)

loutjoint1 <- lmer(CHANGE ~ COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$SAMECOUNTY==1,])
loutjoint2 <- lmer(CHANGE ~ COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$SAMECOUNTY==0,])

loutjoint3 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$SAMECOUNTY==1,])
loutjoint4 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$SAMECOUNTY==0,])

texreg(list(loutjoint1,loutjoint2,loutjoint3,loutjoint4),stars=0.05,custom.model.names=c("Same County","Different County","Same County","Different County"))

#####
### unemployment

lout65p1 <- lm(I(FTWHTS65-FTBLKS65) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout82p1 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.82+COPCBCH.82+COUNEMP.82+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout97p1 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.97+COPCBCH.97+COUNEMP.97+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

lout65mat1 <- summary(lout65p1)$coef
lout82mat1 <- summary(lout82p1)$coef
lout97mat1 <- summary(lout97p1)$coef

#lout65p2 <- lm(I(FTWHTS65-FTBLKS65) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout82p2 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout97p2 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

lout82p3 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.82+COPCBCH.82+COUNEMP.82+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout97p3 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.97+COPCBCH.97+COUNEMP.97+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)

mf82 <- model.frame(lout82p3)
mf97 <- model.frame(lout97p3)

colnames(mf82)[1:7] <- colnames(mf97)[1:7] <- c("CHANGE","COPCNHBL.70","COPCBCH.70","COUNEMP.70","COPCNHBLNOW","COPCBCHNOW","COUNEMPNOW")

mf82$WAVE <- 1
mf97$WAVE <- 2

identical(colnames(mf82),colnames(mf97))

mfjoint <- rbind(mf82,mf97)
library(lme4)

loutjoint1 <- lmer(CHANGE ~ COPCNHBL.70*COUNEMP.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)
loutjoint2 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$COUNEMP.70 < median(mfjoint$COUNEMP.70,na.rm=T),])
loutjoint3 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$COUNEMP.70 > median(mfjoint$COUNEMP.70,na.rm=T),])

loutjoint4 <- lmer(CHANGE ~ COPCNHBL.70*COPCBCH.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)
loutjoint5 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$COPCBCH.70 < median(mfjoint$COPCBCH.70,na.rm=T),])
loutjoint6 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$COPCBCH.70 > median(mfjoint$COPCBCH.70,na.rm=T),])

library(texreg)
texreg(list(loutjoint1,loutjoint2,loutjoint3,loutjoint4,loutjoint5,loutjoint6),stars=c(0.05))#,custom.model.names=c("Later Prejudice","Prej. 82","Prej. 97","Prej. 82","Prej. 97"))

loutjoint1 <- lmer(CHANGE ~ COPCNHBL.70*COUNEMP.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)
#loutjoint1 <- lmer(CHANGE ~ COPCNHBL.70*COUNEMPNOW+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint)

#### INSIDE / OUTSIDE U.S. SOUTH

lout65p1 <- lm(I(FTWHTS65-FTBLKS65) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD+SOUTH,data=dta65sw)
lout82p1 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.82+COPCBCH.82+COUNEMP.82+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD+SOUTH,data=dta65sw)
lout97p1 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.97+COPCBCH.97+COUNEMP.97+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD+SOUTH,data=dta65sw)

lout65mat1 <- summary(lout65p1)$coef
lout82mat1 <- summary(lout82p1)$coef
lout97mat1 <- summary(lout97p1)$coef

#lout65p2 <- lm(I(FTWHTS65-FTBLKS65) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout82p2 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)
lout97p2 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+ATTENDCOL+COLLEGEGRAD,data=dta65sw)

lout82p3 <- lm(I(FTWHTS82-FTBLKS82) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.82+COPCBCH.82+COUNEMP.82+SOUTH65+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)
lout97p3 <- lm(I(FTWHTS97-FTBLKS97) ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBL.97+COPCBCH.97+COUNEMP.97+SOUTH65+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65,data=dta65sw)

mf82 <- model.frame(lout82p3)
mf97 <- model.frame(lout97p3)

colnames(mf82)[1:7] <- colnames(mf97)[1:7] <- c("CHANGE","COPCNHBL.70","COPCBCH.70","COUNEMP.70","COPCNHBLNOW","COPCBCHNOW","COUNEMPNOW")

mf82$WAVE <- 1
mf97$WAVE <- 2

identical(colnames(mf82),colnames(mf97))

mfjoint <- rbind(mf82,mf97)
library(lme4)

loutjoint2 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$SOUTH65==1,])
loutjoint3 <- lmer(CHANGE ~ COPCNHBL.70+COPCBCH.70+COUNEMP.70+COPCNHBLNOW+COPCBCHNOW+COUNEMPNOW+ATTENDCOL+COLLEGEGRAD+PID65+MALE+BLACK+AGE65+FEDYEARS+CATHOLIC65+JEWISH65+(1|WAVE),data=mfjoint[mfjoint$SOUTH65==0,])

texreg(list(loutjoint2,loutjoint3))





